﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MsTestUtilities;
using MsTestUtilities.Utilities;

namespace MsTestUtilitiesTests.ExceptionExpectationTests
{
    [TestClass]
    public class IsSatisfiedForPredicateTests
    {        
        private const string Param = "myParam";

        private ExceptionExpectation<ArgumentNullException> _expector;

        [TestInitialize]
        public void Setup()
        {
            _expector = new ExceptionExpectation<ArgumentNullException>(e => e.ParamName == Param);
        }

        [TestMethod]
        public void ExpectedExceptionThrownSatisfiesPredicate_AssertionHolds()
        {
            var result = _expector.IsSatisfiedFor(() => { throw new ArgumentNullException(Param); });
            Assert.IsTrue(result.AssertionHolds);
        }

        [TestMethod]
        public void ExpectedExceptionThrownWithoutMessage_FailAssertion()
        {
            var result = _expector.IsSatisfiedFor(() => { throw new ArgumentNullException("otherParam"); });
            AssertAssertionError("Predicate did not hold for exception", result);
        }

        private void AssertAssertionError(string expectedMessage, AssertionResult result)
        {
            Assert.IsFalse(result.AssertionHolds);
            StringAssert.StartsWith(result.AssertionError, expectedMessage);
        }
    }
}
